import os
import tarfile

from base_class import MyBase
from abs_file import AbsFile
from app.utils.util_md5 import util_get_md5

RECEIVE_PATH='/home/zhangle/etl/data/original/'
DECOMPRESS_PATH='/home/zhangle/etl/data/decompress/'

class Decompress(MyBase):
    
    def __init__(self):
        MyBase.__init__(self)

    def decompress_data(self):
        self.info('decompress start...')
        files = self.get_compress_files()
        self.decompress_files(files)
        self.info('decompress end.')

    def decompress_files(self, files):
        self.info('decompress files begin')
        if len(files) <= 0:
            raise Exception('decompress file list is empty.')
        for f in files:
            dst_path = self.decompress(f)
            self.check_decompress_result(dst_path)
        self.info('decompress files end')

    def check_decompress_result(self, dst_path):
        self.debug('check_decompress_result %s start...' % dst_path)
        abs_file = AbsFile()
        abs_file_name =  os.path.basename(dst_path) + '.abs'
        abs_file.parse_from_file(abs_file_name)
        
        self.info('check file count...')
        file_cnt = len(os.listdir(dst_path)) 
        if int(abs_file.abs_header['cnt']) is not file_cnt:
            raise Exception('check file count error, expect %d result %d' % (abs_file.abs_header['cnt'], file_cnt))
        self.info('check file count result %d' % file_cnt)
        
        for abs in abs_file.abs_list:
            dat_path =  os.path.join(dst_path, abs['name'])
            if not os.path.exists(dat_path):
                raise Exception('dat file not exist')
            dat_md5 = util_get_md5(dat_path)
            print dat_path, dat_md5
        pass
        
        self.debug('check_decompress_result end...')
        pass
    
    def decompress(self, file):
        self.debug('decompress file %s begin' % file)
        src = os.path.join(RECEIVE_PATH, file)
        dst = os.path.join(DECOMPRESS_PATH, file[:-4])
        self.debug('decompressing from %s to %s.' % (src, dst))

        tar = tarfile.open(src, "r:gz")
        tar.extractall(dst)
        tar.close()

        self.debug('decompress file %s end' % file)
        return dst
    
    def get_compress_files(self):
        files = os.listdir(RECEIVE_PATH)
        for f in files:
            if f[-4:] != '.tgz':
                files.remove(f)
        self.info('get_compress_files:%s' % str(files)) 
        return files

if __name__ == '__main__':
    d = Decompress()
    d.decompress_data()
